using System.Linq;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Windowing;
using WikiApp.Lib;

namespace iOSClub.WikiApp.Views;

public partial class MainWindow : AppWindow
{
    public readonly GraphWiki _graphWiki = new();
    private PageItem[] _pageList = [];
    private int _this;
    private int _parent;

    public MainWindow()
    {
        InitializeComponent();

        TitleBar.ExtendsContentIntoTitleBar = true;
        TitleBar.TitleBarHitTestType = TitleBarHitTestType.Complex;

        FrameView.NavigationPageFactory = new NavigationPageFactory();
    }

    protected override async void OnInitialized()
    {
        var init = await _graphWiki.Init();

        _pageList = init.Pages.List;
        SearchBox.ItemsSource = _pageList;
        var nav = init.Pages.Tree.Select(object (x) => new NavigationViewItem()
        {
            Content = x.Title,
            Tag = x,
            IconSource = x.IsFolder
                ? new SymbolIconSource() { Symbol = Symbol.Folder }
                : new SymbolIconSource() { Symbol = Symbol.Document }
        }).ToList();
        NavView.MenuItems = nav;
        Navigate("home");
    }

    private void Navigate(string path)
    {
        if (path is "Setting" or "Help" or "Site")
        {
            FrameView.NavigateFromObject(path);
            return;
        }

        var item = _pageList.FirstOrDefault(x => x.Path == path);
        if (item == null) return;
        Navigate(item);
    }

    private async void Navigate(PageTree item)
    {
        if (item.IsFolder)
        {
            var init = await _graphWiki.GetPageTree(item.Id);
            var nav = init.Select(object (x) => new NavigationViewItem()
            {
                Content = x.Title,
                Tag = x,
                IconSource = x.IsFolder
                    ? new SymbolIconSource() { Symbol = Symbol.Folder }
                    : new SymbolIconSource() { Symbol = Symbol.Document }
            }).ToList();
            NavView.MenuItems = nav;
            _parent = item.Parent;
            _this = item.Id;
            Button.IsVisible = true;
            return;
        }

        var p = _pageList.FirstOrDefault(x => x.Path == item.Path);
        if (p == null) return;
        Navigate(p);
    }

    private void Navigate(object o)
    {
        FrameView.NavigateFromObject(o);
    }

    private async void BackClick(object? sender, RoutedEventArgs e)
    {
        var init = await _graphWiki.GetPageTree(_parent);
        var nav = init.Select(object (x) => new NavigationViewItem()
        {
            Content = x.Title,
            Tag = x,
            IconSource = x.IsFolder
                ? new SymbolIconSource() { Symbol = Symbol.Folder }
                : new SymbolIconSource() { Symbol = Symbol.Document }
        }).ToList();
        NavView.MenuItems = nav;
        var f = init.FirstOrDefault();
        if(f == null)return;
        _parent = f.Parent;
        _this = _parent;
    }

    private void ItemInvoked(object? sender, NavigationViewItemInvokedEventArgs e)
    {
        switch (e.InvokedItemContainer.Tag)
        {
            case string s:
            {
                if (string.IsNullOrEmpty(s)) return;
                Navigate(s);
                return;
            }
            case PageTree p:
            {
                Navigate(p);
                return;
            }
        }
    }

    private void SearchChanged(object? sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count == 0) return;
        var a = e.AddedItems[0] as PageItem;
        Navigate(a!.Path);
    }
}